package com.spring.framework.webmvc.servlet;

import java.io.File;

/**
 * @author ππ
 * @date 2020-11-17 13:58
 * @desc
 */

public class MyViewResolver {
    private final String DEFAULT_TEMPLATE_SUFFIX=".html";
    private String templateRootPath;

    public MyViewResolver(String templateRoot) {
        String templateRootPath = this.getClass().getClassLoader().getResource(templateRoot).getFile();
        this.templateRootPath = templateRootPath;
    }

    public MyView resolveViewName(String viewName) {
        if(viewName == null || "".equals(viewName.trim())){
            return null;
        }
        viewName = viewName.endsWith(DEFAULT_TEMPLATE_SUFFIX) ? viewName : viewName + DEFAULT_TEMPLATE_SUFFIX;
        File file = new File(this.templateRootPath+"/"+viewName);
        return new MyView(file);
    }
}
